﻿using Microsoft.AspNetCore.Mvc;

namespace HuiLian.Generate
{
    /// <summary>
    /// 代码生成器
    /// </summary>
    public class Creater
    {
        //private static readonly string entityRootPath = @"D:\MyProjects\XinHua\HuiLian.Server\HuiLian.Repository\";
        private static readonly string entityRootPath = @"E:\shuihuanjing\HuiLian.Repository\Platform\";
        private static readonly string serviceRootPath = @"E:\shuihuanjing\HuiLian.Services\Platform\";
        private static readonly string controllerRootPath = @"E:\shuihuanjing\HuiLian.Web\Controllers\Platform\";

        /// <summary>
        /// 开始生成代码
        /// <paramref name="folder">实体所在的文件夹名</paramref>
        /// </summary>
        public static void Do(string folder, string Name)
        {
            if (!Directory.Exists(entityRootPath))
            {
                Directory.CreateDirectory(entityRootPath);
            }
            string entityPath = Path.Combine(entityRootPath, folder);
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CodeTemplate");

            var entities = new string[1] { $"{folder}Entity" };//Directory.GetFiles(entityPath);
            var templates = Directory.GetFiles(templatePath);
            var put = Directory.GetFiles(@$"{templatePath}\Dto\");
            templates = put.Concat(templates).ToArray();


            if (entities != null && entities.Length > 0 && templates != null && templates.Length > 0)
            {
                foreach (var tmp in templates)
                {
                    var tmpName = Path.GetFileName(tmp);
                    string path = tmp.Contains("Repository") ? entityRootPath : serviceRootPath;
                    path = tmp.Contains("Controller") ? controllerRootPath : path;
                    path = tmp.Contains("_MapConfig") ? serviceRootPath : path;
                    if (!tmp.Contains("Controller"))
                        path = Path.Combine(path, folder);
                    if (tmp.Contains("Input"))
                    {
                        path += @"\Input";
                    }

                    var args = new CodeArgs
                    {
                        TemplateFile = tmp,
                        SaveFile = Path.Combine(path, Path.GetFileName(tmp).Replace(".txt", ".cs")),
                        Overflow = false,
                        Namespace = folder
                    };
                    foreach (var entity in entities.Where(w => w.Contains("Entity")))
                    {
                        var entityFileName = Path.GetFileName(entity);
                        var entityName = entityFileName.Replace("Entity", String.Empty).Replace(".cs", String.Empty);

                        args.EntityName = entityName;
                        args.SaveFile = String.Format(args.SaveFile, entityName);
                        CreateCode(args, Name);
                    }
                }
            }
        }

        /// <summary>
        /// 开始生成代码
        /// <paramref name="folder">实体所在的文件夹名</paramref>
        /// </summary>
        public static void DoDto(string folder)
        {
            string entityPath = Path.Combine(entityRootPath, folder);
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CodeTemplate\\Dto");

            var entities = Directory.GetFiles(entityPath);
            var templates = Directory.GetFiles(templatePath);

            if (entities != null && entities.Length > 0 && templates != null && templates.Length > 0)
            {
                foreach (var tmp in templates)
                {
                    var tmpName = Path.GetFileName(tmp);
                    string path = serviceRootPath;

                    path = Path.Combine(path, folder, "Dto");

                    var args = new CodeArgs
                    {
                        TemplateFile = tmp,
                        SaveFile = Path.Combine(path, Path.GetFileName(tmp).Replace(".txt", ".cs")),
                        Overflow = false,
                        Namespace = folder
                    };
                    foreach (var entity in entities.Where(w => w.Contains("Entity")))
                    {
                        var entityFileName = Path.GetFileName(entity);
                        var entityName = entityFileName.Replace("Entity", String.Empty).Replace(".cs", String.Empty);

                        args.EntityName = entityName;
                        args.SaveFile = String.Format(args.SaveFile, entityName);
                        CreateCode(args);
                    }
                }
            }
        }

        #region 生成代码
        /// <summary>
        /// 生成仓库接口代码
        /// </summary>
        /// <param name="args"></param>
        private static void CreateCode(CodeArgs args, string Name = "XX")
        {

            var template = File.ReadAllText(args.TemplateFile);
            var content = template
                .Replace("$Entity", args.EntityName)
                .Replace("$_Entity", args.FirstLetterToLower)
                .Replace("$Namespace", args.Namespace)
                .Replace("XXX", Name);

            if (!Directory.Exists(args.SavePath))
            {
                Directory.CreateDirectory(args.SavePath);
            }

            if (!File.Exists(args.SaveFile) || args.Overflow)
            {
                File.WriteAllText(args.SaveFile, content);
            }
        }

        #endregion
    }
}
